package la.iok.hzsvn.lewin.movie.operation.controller;

import la.iok.hzsvn.lewin.movie.core.RoleEnum;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.core.model.PlayRecordVo;
import la.iok.hzsvn.lewin.movie.operation.entity.PlayRecord;
import la.iok.hzsvn.lewin.movie.operation.model.EndParam;
import la.iok.hzsvn.lewin.movie.operation.model.PlayRecordAutoEndCfg;
import la.iok.hzsvn.lewin.movie.operation.model.PlayRecordParam;
import la.iok.hzsvn.lewin.movie.operation.model.PreviewLuckyMoney;
import la.iok.hzsvn.lewin.movie.operation.service.PlayRecordService;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import la.iok.hzsvn.share.tools.datetime.DateTimeUtil;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static la.iok.hzsvn.lewin.movie.core.exception.ErrorCode.HAVE_OTHER_PLAYING_RECORD;

@RestController
@RequestMapping("playRecords")
public class PlayRecordController {
    private final PlayRecordService playRecordService;

    public PlayRecordController(PlayRecordService playRecordService) {
        this.playRecordService = playRecordService;
    }

    @PostMapping
    public SingleDataResponse<Long> save(@RequestHeader Token token, @RequestBody PlayRecordParam param){
        List<PlayRecord> records = playRecordService.playingRecords(token);
        HAVE_OTHER_PLAYING_RECORD.assertEmpty(records,()->new Object[]{records.size()});
        return new SingleDataResponse<>(playRecordService.start(token,param));
    }
    @PutMapping("end")
    public OperatorResponse end(@RequestHeader Token token, @RequestBody EndParam param){
        playRecordService.end(token,param);
        return OperatorResponses.success();
    }
    @GetMapping("playing")
    public ListDataResponse<PlayRecordVo> playingRecords(@RequestHeader Token token){
        return new ListDataResponse<>(playRecordService.po2vo(playRecordService.playingRecords(token)));
    }

    @GetMapping("enter/{id}")
    public SingleDataResponse<PlayRecordVo> enter(@RequestHeader Token token,@PathVariable Long id){
        return new SingleDataResponse<>(playRecordService.enter(token,id));
    }

    @GetMapping
    public SingleDataResponse<PageVo<PlayRecordVo>> page(
            @RequestHeader Token token, String orderColumn, String order, int page, int limit, String movieName,
            Long orgUnitId, Long roomId, Long userId, String start, String end){
        page = Math.max(1,page);
        limit = Math.min(Math.max(1,limit),50);
        Date s = DateTimeUtil.safelyParseDate(start,"yyyy-MM-dd");
        Date e = DateTimeUtil.safelyParseDate(end,"yyyy-MM-dd");
        if(s != null){
            s = DateTimeUtil.setTime(s,0,0,0,0);
        }
        if(e != null){
            e = DateTimeUtil.setTime(e,23,59,59,999);
        }
        PageVo<PlayRecordVo> pageRecords=playRecordService.page(token,page,limit,orderColumn,order,movieName,orgUnitId,roomId,userId,s,e);
        return new SingleDataResponse<>(pageRecords);
    }

    @PostMapping("returnMoney")
    public ListDataResponse<Set<Long>> returnMoney(@RequestHeader Token token){
        ErrorCode.AUTHORITY_ERROR.assertTrue(token.hasAuthority(RoleEnum.Admin),RoleEnum.Admin.getDisplayName());
        // 失败的账号
        Set<Long> excludes = new HashSet<>();
        // 成功发放返利的账号
        Set<Long> success = new HashSet<>();
        // 开本记录不达标的账号
        Set<Long> lessAmount = new HashSet<>();
        playRecordService.processReturnMoney(excludes, success, lessAmount);
        return new ListDataResponse<>(Arrays.asList(success, lessAmount, excludes));
    }

    @GetMapping("previewReturnMoney")
    public SingleDataResponse<PreviewLuckyMoney> previewReturnMoney(@RequestHeader Token token){
        return new SingleDataResponse<>(playRecordService.previewReturnMoney(token));
    }

    @GetMapping("autoEndCfg")
    public SingleDataResponse<PlayRecordAutoEndCfg> autoEndConfig(){
        return new SingleDataResponse<>(playRecordService.cfg());
    }

    @GetMapping("{id}/remainingTime")
    public SingleDataResponse<Integer> remainingTime(@RequestHeader Token token, @PathVariable Long id){
        return new SingleDataResponse<>(playRecordService.remainingTime(token, id));
    }
}
